//
//  ZCOderReceiptVC.m
//  ZCJZ
//
//  Created by jzwp on 2020/1/2.
//  Copyright © 2020 jeking. All rights reserved.
//

#import "ZCOderReceiptVC.h"
#import "ZCOrderViewModel.h"
#import "WZGOderReceiptModel.h"
#import "PaymentManager.h"
#import <MJExtension.h>
#import <UIImageView+WebCache.h>
@interface ZCOderReceiptVC ()

@property (nonatomic,strong) WZGOderReceiptModel *dataModel;

@end

@implementation ZCOderReceiptVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [generalToolBox cutTheRoundedForView:self.allView withCornerRadius:7];
    [generalToolBox cutTheRoundedForView:self.shopImg withCornerRadius:2];

    [self netFetchOrderDetail];
}

//-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//    [self dismissViewControllerAnimated:YES completion:nil];
//}

- (IBAction)wxFrendAction:(id)sender {
    UIImage *image = [self coverAllViewImg];
    [[PaymentManager sharedManager] share2WeiXinFirendList:image];
}

- (IBAction)saveImgAction:(id)sender {
    UIImage *image = [self coverAllViewImg];
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}

- (IBAction)backAction:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (void)netFetchOrderDetail {
    __weak typeof(self) weakSelf = self;
    
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [ZCOrderViewModel getOderReceiptWithOderNum:self.ids complete:^(ZCErrorCode status, NSString *message, id data) {
        [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
        if (status == kHXSNoError) {
            weakSelf.dataModel = [WZGOderReceiptModel mj_objectWithKeyValues:data];
            [weakSelf setupUI];
        } else if (status == kHXSInvalidTokenError) {
//            WZGLoginVC *vc = [[WZGLoginVC alloc] init];
//            [vc setGoodsVCCallBack:^(NSInteger type) {
//                [weakSelf netFetchOrderDetail];
//            }];
//            UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:vc];
//            nvc.modalPresentationStyle = UIModalPresentationFullScreen;
//            [weakSelf.navigationController presentViewController:nvc animated:YES completion:nil];
        } else {
            [weakSelf.view makeToast:message duration:2.0 position:CSToastPositionCenter];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [weakSelf dismissViewControllerAnimated:YES completion:nil];
            });
        }
    }];
}

- (void)setupUI{
    [self.shopImg sd_setImageWithURL:[NSURL URLWithString:self.dataModel.goods_thumb]
    placeholderImage:nil
             options:SDWebImageAllowInvalidSSLCertificates];
    self.shopNameLabel.text = self.dataModel.goods_title;
    self.shopInfoLabel.text = self.dataModel.sku_properties_name;
    
    self.shopNumLabel.text = [NSString stringWithFormat:@"x%@",self.dataModel.nums];
    self.shopPriceLabel.text = [NSString stringWithFormat:@"¥%@",self.dataModel.goods_fee];
    self.postPriceLabel.text = [NSString stringWithFormat:@"¥%@",self.dataModel.freight_fee];
    self.oderPriceLabel.text = [NSString stringWithFormat:@"¥%@",self.dataModel.actual_fee];
    
    self.nameLabel.text = [NSString stringWithFormat:@"%@  %@",self.dataModel.receiver_name,self.dataModel.receiver_mobile];
    self.adressLabel.text = self.dataModel.receiver_address;
    self.statusLabel.text = self.dataModel.status_name;
    self.oderNumLabel.text = self.dataModel.order_sn;
    self.oderTimeLabel.text = self.dataModel.created_at;
}

- (void)managerDidRecvShareResponse:(SendMessageToWXResp *)response {
    if (response.errCode == 0) {
        [self.view makeToast:@"分享成功" duration:2.0 position:CSToastPositionCenter];
    } else {
        [self.view makeToast:@"分享失败" duration:2.0 position:CSToastPositionCenter];
    }
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    if (error) {
        [self.view makeToast:@"保存失败" duration:2.0 position:CSToastPositionCenter];
    } else {
        [self.view makeToast:@"保存成功" duration:2.0 position:CSToastPositionCenter];
    }
}

- (UIImage *)coverAllViewImg {
    UIGraphicsBeginImageContextWithOptions(self.coverView.bounds.size, NO, 0.0);
    // 绘制图片
    [self.coverView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

@end
